﻿using System.Collections.Generic;
using System.Linq;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Common;
using KLS.PPDS.Data.Database.Repository.RepositoryInterfaces;

namespace KLS.PPDS.Data.Database.Repository.EntityRepository
{
    public class CompanyRepository: LinqRepository<Company>, ICompanyRepository 
    {
        public Company GetCompany(string companyName)
        {
            var currentSession = NHibernateSessionManager.Instance.GetSession();
            var query = string.Format(@"from Company c where c.CompanyName=:companyName");
            return (Company)currentSession.CreateQuery(query).SetParameter("companyName", companyName).UniqueResult();
           
        }
       
        
        public Company GetCompanyByNo(string companyNo)
        {
            var currentSession = NHibernateSessionManager.Instance.GetSession();
            var query = string.Format(@"from Company c where c.CompanyNo=:companyNo");
            var comp = currentSession.CreateQuery(query).SetParameter("companyNo", companyNo).List();
            if (comp != null && comp.Count>0)
                return (Company) comp[0];
            return null;

        }

       public List<Company>  GetAllCompany()
       {

           return FindAll().Where(c => c.IsDeleted == false).OrderBy(c => c.CompanyName).ToList();
       }

        public  Company GetCompany(int companyId)
        {
            return FindAll().Where(c => c.Id == companyId).FirstOrDefault();
        }

        public List<Company> GetAllCompany(int id)
        {
            if(id == 0)
            {
                return GetAllCompany();
            }

            return FindAll().Where(c => c.Id == id && c.IsDeleted.Equals(false)).OrderBy(c => c.CompanyName).ToList();
        }

        public List<Company> GetAllCompanyGraphicsUserId(int id)
        {            
            return FindAll().Where(c =>  c.IsDeleted.Equals(false) && c.Graphic.HasValue.Equals(true) && c.Graphic == id ).OrderBy(c => c.CompanyName).ToList();
        }
    }
}
